package com.android.clinic.doctors.app.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.base.BaseActivity;
import com.android.clinic.doctors.app.entity.UserInfo;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.utils.BaseTextWatcher;
import com.android.clinic.doctors.app.utils.Utils;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 编辑公告
 * Created by cheng on 2018/6/12.
 */
public class EditPlacardActivity extends BaseActivity {

    private EditText etNotice;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_edit_placard;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("编辑公告");
        etNotice = findViewById(R.id.et_notice);
        etNotice.addTextChangedListener(new AskTextWatcherListener());
    }

    @Override
    protected void loadData() {
        super.loadData();
        HttpClient.getHttpService().findNotice(ClinicDoctorsApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<UserInfo>() {
                    @Override
                    public void onSuccess(UserInfo userInfo) {
                        if (!Utils.isEmpty(userInfo.getNotice())) {
                            etNotice.setText(userInfo.getNotice());
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        showToast(message);
                    }
                });
    }

    private class AskTextWatcherListener extends BaseTextWatcher {

        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            temp = s;
        }

        @Override
        public void afterTextChanged(Editable editable) {
            editStart = etNotice.getSelectionStart();
            editEnd = etNotice.getSelectionEnd();
            if (temp.length() > 150) {//限制长度
                showToast("输入的字数已经超过了限制！");
                editable.delete(editStart - 1, editEnd);
                int tempSelection = etNotice.length();
                etNotice.setText(editable);
                etNotice.setSelection(tempSelection);
            }
        }
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        if (v.getId() == R.id.btn_save) {
            String notice = etNotice.getText().toString().trim();
            if (TextUtils.isEmpty(notice)) {
                showToast("公告内容不能为空!");
                return;
            }
            updateNotice(notice);
        }
    }

    /**
     * 编辑公告
     * @param notice
     */
    private void updateNotice(String notice) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", ClinicDoctorsApplication.getUserInfo().getId());
            jsonObject.put("notice", notice);

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());
            HttpClient.getHttpService().updateNotice(body)
                    .compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers(loading, "正在提交..."))
                    .subscribe(new BaseObserver<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showToast("提交成功!");
                            finish();
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                            finish();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
